<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公众号
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'公众号列表'),
	array('op'=>'edit','title'=>'添加公众号'),	
);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['nickname'])){
		$data['uniacid']=$_SESSION['uniacid'];
		$data['nickname like']='%'.trim($_RQ['nickname']).'%';
		$mid=pdo_get('jy_wechatnews_member',$data);
		$params['mid']=$mid['mid'];
		$return['params']['mid']=$mid['mid'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['id'])){
		$params['id']=intval($_RQ['id']);
		$return['params']['id']=$_RQ['id'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_wechatnews_list',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['merchname']=pdo_getcolumn('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['merchid']),'merchname');
			$item['merchmid']=pdo_getcolumn('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['merchid']),'mid');
			$item['merchmember']=model('core/member')->detail($item['merchmid'],array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
			$item['likenum']=pdo_count('jy_wechatnews_shoucang',array('listid'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'isshoucang'=>1),'id');
			$item['ordernum']=pdo_count('jy_weishop_order',array('merchid'=>$item['merchid'],'uniacid'=>$_SESSION['uniacid'],'ispay'=>1),'id');
			$item['ordernum']++;
			$item['isadnum']=pdo_count('jy_wechatnews_newslog',array('listid'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'ispay'=>1),'id');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'title','title'=>'公众号名称','type'=>'string'),
			array('name'=>'group_id','title'=>'公众号分类','type'=>'select','options'=>'groups'),
			array('type'=>'string','title'=>'发布商家','name'=>'merchname'),
			array('type'=>'string','title'=>'商家金币','name'=>'merchmember.credit3'),
			array('type'=>'string','title'=>'商家余额','name'=>'merchmember.credit2'),
			array('name'=>'readnum','title'=>'阅读量','type'=>'string'),
			array('name'=>'likenum','title'=>'收藏量','type'=>'string'),
			array('name'=>'ordernum','title'=>'总名额','type'=>'string'),
			array('name'=>'isadnum','title'=>'已用名额','type'=>'string'),
			array('name'=>'jinbi','title'=>'奖励金币/次','type'=>'string'),
			array('name'=>'price','title'=>'奖励佣金/次','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
			array('name'=>'updatetime','title'=>'刷新时间','type'=>'time'),
			array('name'=>'createtime','title'=>'发布时间','type'=>'time'),
		),'action' =>array(
			array('type'=>'post','op'=>'ok','show'=>array('status'=>array(0,2)),'title'=>'审核通过','message'=>'确定通过该公众号吗！'),
			array('type'=>'post','op'=>'no','show'=>array('status'=>array(0,1)),'title'=>'不通过','message'=>'确定忽略该公众号吗！'),
		)),
		'search'=>array(
			array('name'=>'id','width'=>15,'title'=>'公众号ID','type'=>'string'),
			array('name'=>'nickname','width'=>15,'title'=>'发布人昵称','type'=>'string'),
			array('name'=>'title','width'=>15,'title'=>'公众号标题','type'=>'string'),
		)
	);
	$return['groups']=pdo_getall('jy_wechatnews_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	$return['isfen']=array('不支持','支持');
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_wechatnews_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));		
	}else{
		$item=array('type'=>$this->session('type'),'star'=>3,'isnews'=>0);
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回公众号列表'),
		),
		'edit'=>array(
					array('name'=>'title','title'=>'公众号名称','type'=>'string'),
					array('name'=>'appid','title'=>'公众号appid','type'=>'string'),
					array('name'=>'appsecret','title'=>'公众号密钥','type'=>'string'),
					array('name'=>'image','title'=>'公众号图片','type'=>'image'),
					array('type'=>'choose','title'=>'选择商户','name'=>'merchid','action'=>'jy_merch/get_merch/','choose_title'=>'选择商户'),
					array('name'=>'group_id','title'=>'主分类','type'=>'select2','options'=>'groups','name2'=>'cid','title2'=>'子分类','options2'=>'cids'),
					array('name'=>'desc','title'=>'公众号介绍','type'=>'text'),
					array('name'=>'jinbi','title'=>'奖励金币/次','type'=>'string'),
					array('name'=>'price','title'=>'奖励佣金/次','type'=>'string'),
					array('name'=>'readnum','title'=>'查看人数','type'=>'string'),
					array('name'=>'ishot','title'=>'推荐？','type'=>'switch'),
					array('name'=>'status','title'=>'状态','type'=>'switch'),
		)
	);
	$return['groups']=pdo_getall('jy_wechatnews_group',array('uniacid'=>$_SESSION['uniacid'],'pid'=>0),array('id','title'),'id');
	foreach($return['groups'] as &$gv){
		$gv['cids']=pdo_getall('jy_wechatnews_group',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$gv['id']),array('id','title'),'id');
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['merchid']){
		exi('请选择发布的商户！','error');
	}
	if(!$postdata['title']){
		exi('请填写公众号名称！','error');
	}
	if(!$postdata['productsn']){
		//exi('请填写公众号编码！','error');
	}
	if(!$postdata['image']){
		//exi('请填写公众号图片！','error');
	}
	if(!$postdata['price']){
		//exi('请填写公众号发文价/次！','error');
	}
	if(!$postdata['group_id']){
		exi('请填写公众号分类！','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'image'=>trim($postdata['image']),
		'merchid'=>intval($postdata['merchid']),
		'desc'=>trim($postdata['desc']),
		'readnum'=>intval($postdata['readnum']),
		'cid'=>intval($postdata['cid']),
		'group_id'=>intval($postdata['group_id']),
		'status'=>intval($postdata['status']),
		'jinbi'=>trim($postdata['jinbi']),
		'price'=>trim($postdata['price']),
		'noshow'=>intval($postdata['noshow']),
		'ishot'=>intval($postdata['ishot']),
		'appid'=>trim($postdata['appid']),
		'appsecret'=>trim($postdata['appsecret']),
		'expire'=>0,
		'up_tokentime'=>0,
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_wechatnews_list',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		$data['updatetime']=TIMESTAMP;
		pdo_insert('jy_wechatnews_list',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择公众号！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_wechatnews_list',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='no'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要忽略的公众号！','error');
	}
	pdo_update('jy_wechatnews_list',array('status'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	exi('已忽略该公众号！');
}
if($_RQ['op']=='ok'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要审核的公众号！','error');
	}
	pdo_update('jy_wechatnews_list',array('status'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	exi('已审核通过该公众号！');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_wechatnews_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}